const db = wx.cloud.database()

Page({
    data: {
        fileinfo: {
            fileImg: []
        },
    },
    // 上传文件
    chooseFile() {
        var that = this
        let fileinfo = this.data.fileinfo
        wx.chooseMessageFile({
            count: 1,
            type: 'file',
            success(res) {
                wx.showLoading({
                    title: '文件上传中...',
                })
                // 产生6位随机数，防止文件名冲突
                var randString = Math.floor(Math.random() * 1000000).toString()
                var filepath = randString + filename
                // 文件上传至存储
                wx.cloud.uploadFile({
                    cloudPath: 'file/' + filepath,
                    filePath: res.temFilePaths[0].path,
                }).then(res => {
                    var filename1 = filename1.lastIndexOf(".");
                    var index2 = filename1.length;
                    var type = filename1.substring(index1, index2);
                    var FileImg = []
                    if (type == '.pdf') {
                        FileImg[0] = '/images/pdf.png'
                    } else if (type == '.doc' || type == '.docx') {
                        FileImg[0] = '/images/word.png'
                    } else if (type == '.xls' || type == '.xlsx') {
                        FileImg[0] = '/images/excel.png'
                    } else {
                        FileImg[0] = '/images/file.png'
                    }
                    fileinfo.fileID = res.fileID
                    fileinfo.filename = filename
                    fileinfo.fileImg = FileImg
                    that.setData({
                        fileinfo: fileinfo
                    })
                    wx.hideLoading({
                        success: (res) => {},
                    })
                }).catch(error => {
                    // handle error
                })
            }
        })
    },
    // 删除文件
    deleteFile: function (event) {
        let fileinfo = this.data.fileinfo
        var fileID = fileinfo.fileID
        console.log(fileID)
        wx.cloud.deleteFile({
            fileList: [fileID],
        }).then(res => {
            fileinfo.filename = ''
            fileinfo.fileImg = []
            this.setData({
                fileinfo,
                fileinfo
            }).catch(error => {
                // handle error
            })
        })
    },
    submitform: function (event) {
        let fileinfo = this.data.fileinfo
        db.collection('files').add({
            // data字段表示需要新增的JSON数据
            data: {
                title: fileinfo.title,
                content: fileinfo.content,
                fileID: fileinfo.fileID,
                fileImg: fileinfo.fileImg,
                filename: fileinfo.filename,
                submitdate: db.serverDate()
            }
        }).then(res => {
            wx.showToast({
                title: '文件上传成功，请尽快缴费',
                icon: 'success',
                duration: 2000
            })
            // wx.navigateTo({
            //   url: 'url',
            // })
        }).catch()
    },
})


